package com.ayit.car.controller;

import com.ayit.car.common.result.PageResult;
import com.ayit.car.common.result.Result;
import com.ayit.car.pojo.Announcements;
import com.ayit.car.pojo.dto.AnnouncementPageQueryDTO;
import com.ayit.car.service.AnnouncementsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/announcement")
@Api(tags = "公告管理")
@Slf4j
public class AnnouncementController {

    @Autowired
    private AnnouncementsService announcementsService;

    @PostMapping("/add")
    @ApiOperation(value = "新增公告")
    public Result add(@RequestBody Announcements announcements){
        log.info("新增公告:{}", announcements);
        announcementsService.save(announcements);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询公告")
    public Result<PageResult> page(AnnouncementPageQueryDTO announcementPageQueryDTO) {
        log.info("分页查询公告:{}", announcementPageQueryDTO);
        PageResult pageResult = announcementsService.getPage(announcementPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "查询公告详情")
    public Result getById(Long id){
        log.info("查询公告详情:{}", id);
        Announcements byId = announcementsService.getById(id);
        return Result.success(byId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询公告")
    public Result list(){
        log.info("查询公告");
        return Result.success(announcementsService.list());
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除公告")
    public Result delete(Long id){
        log.info("删除公告:{}", id);
        announcementsService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改公告")
    public Result update(@RequestBody Announcements announcements){
        log.info("修改公告:{}", announcements);
        announcementsService.updateById(announcements);
        return Result.success();
    }
}
